//============================================================================
// Name        : top_region.cpp
// Author      : dfoelber
//============================================================================

// INCLUDES //
#include "top_region.h"
#include "application.h"
#include "image_manager.h"
#include "render_manager.h"
#include "graphics/graphics.h"
#include <iostream>
#include <sstream>
//////////////

using namespace std;

/**
* Constructor
**/
TopRegion::TopRegion() :
	counter(0),
	threshold(0),
	callback(NULL)
{
	primaryText = new Object(ImageManager::GetManager()->CreateImage(0,0), OBJECT_ID_NONE, true, false);
	primaryText->GetPath()->SetPosition(0,0);
	primaryText->SetVisible(false);
	AddObject(primaryText);
}

/**
* Destructor
**/
TopRegion::~TopRegion()
{
	if( primaryText )
		delete primaryText;
}

/**
* Notifies of time passing.
* 
* Overridden from Region.
* 
* @param The amount of time, in milliseconds, that passed since the last call
**/
void TopRegion::TimePassed( int ms )
{
	if( callback != NULL )
	{
		counter += ms;
		
		if( counter >= threshold )
		{
			callback->TimerCompleted();
			Reset();
		}
	}
}

/**
* Resets the top region.
**/
void TopRegion::Reset()
{
	primaryText->SetVisible(false);
	RenderManager::GetManager()->SetDarken(false);
	
	counter = 0;
	threshold = 0;
	callback = NULL;
}

/**
* Sets the region in plain text mode.
* 
* @param text The text to show
**/
void TopRegion::ShowPlainText( string text )
{
	Reset();
	
	int width = 0, height = 0;
	Graphics::GetGraphics()->GetFontSize( FONT_DEJA_VU_SANS_BOLD, text, FONT_SIZE_LARGE, &width, &height );
	
	Surface* surface = ImageManager::GetManager()->CreateImage(width, height);
	Graphics::GetGraphics()->DrawFont(surface, text, FONT_DEJA_VU_SANS_BOLD, FONT_SIZE_LARGE, COLOR_WHITE, 0, 0);
	
	primaryText->SetSurface( surface, true );
	primaryText->SetPosition( SCREEN_WIDTH / 2 - width / 2, SCREEN_HEIGHT / 2 - height - 2 );
	primaryText->SetVisible( true );
	
	RenderManager::GetManager()->SetDarken(true);
}

/**
* Sets the region in plain text mode on a timer
* 
* @param text The text to show
**/
void TopRegion::ShowPlainText( string text, int time, TopRegionListener* callback )
{
	ShowPlainText( text );
	
	threshold = time;
	this->callback = callback;
}
